module HMS_eclock (key_s,key_m,key_h,clk_12M,rst,CS2,qLEDaddr,beep
);
	input  clk_12M;     //秒频
	wire   clk_disp;//显示频率300
	wire   clk;   //1Hz
	input  rst;		//高电平复位
	input  key_s;
	input  key_m;
	input  key_h;
	
	output beep;
	output [7:0] qLEDaddr;
	output [5:0] CS2;//低电平有效


	wire [2:0] CS;
	wire [3:0] h2;
	wire [3:0] h1;
	wire [3:0] m2;
	wire [3:0] m1;
	wire [3:0] s2;
	wire [3:0] s1;
	wire [3:0] q;//BCD码

 //reg [3:0] s11;
 reg [3:0] m11=4'd0;
 reg [3:0] h11=4'd0;
 reg dire=1;

always @(posedge clk ) //不行就用key_s||key_m||key_h
    begin
		if(key_s==1'b1)  dire=~dire;//加减设置
		if (dire) 
					begin
							if(key_m==1'b1)  m11=(m11==4'd9)?4'd1:(m11+4'd1);							
							if(key_h==1'b1)  h11=(h11==4'd9)?4'd1:(h11+4'd1);
					end
		else
					begin
							if(key_m==1'b1)  m11=(m11==4'd0)?4'd1:(m11-4'd1);							
							if(key_h==1'b1)  h11=(h11==4'd0)?4'd1:(h11-4'd1);
					end
						
    end	
frequency f1(            //默认输出频率为1Hz
					.clk(clk_12M),
					.rst(rst),
					.output_clk(clk)
);	
    
frequency  f2(           //默认输出频率为300Hz
					.clk(clk_12M),
					.rst(rst),
					.output_clk(clk_disp)
);	
 defparam  f2.clk_freq =12000000 ; 		 //输入频率
 defparam  f2.OUT_Freq =300;            //目标频率
 defparam  f2.N =16 ;                   //计数器位数	           
//时分秒计数模块	
	hmscounter hms (
					.clk(clk),
					.rst(rst),
					.h2(h2),
					.h1(h1),
					.m2(m2),
					.m1(m1),
					.s2(s2),
					.s1(s1));
					
	
	
//从时分秒的低位到高位动态显示	
	dyndisp dyndisp1 (
					.clk_disp(clk_disp),
					.rst(rst),
					.h2(h2),
					.h1(h11+h1),
					.m2(m2),
					.m1(m11+m1),
					.s2(s2),
					.s1(s1),
					.qhms(q),    //动态记录当前时分秒的结果
					.qnum(CS)); //动态记录当前时分秒的显示的位置，例如0表示秒的低位，1表示秒的高位，3表示的分的低位

//转换时分秒的BCD码显示	
	disp_decoder BCDshow (            
					.hmsdata(q),     //上一个模块的当前时刻时分秒作为输入
					.qout(qLEDaddr)); 
	decoder_38  test36 (
					.cs(CS),
					.cs2(CS2));
//整点报时模块
   Alarm    alm(
				  
				  .clk(clk),
				  .clk_500Hz(clk_disp),
				  //.clk_1Hz(clk),
				//   .alarm(alarm),
				  .minute({m11+m1,m2}),
				  .second({s1,s2}),
				  .beep(beep));					
			 		     
endmodule